package desktop.lan.remote.key;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import pulpcore.Input;

import desktop.conf.key.KeyConf;

@SuppressWarnings("serial")
public class KeyRemoteImpl extends UnicastRemoteObject implements KeyRemote {
	private static final int UN_PRESSED = -1;
	private KeyConf keyConf;
	private int keyPressed;

	public KeyRemoteImpl(KeyConf keyConf) throws RemoteException {
		this.keyConf = keyConf;
	}

	public void searchKeyPressed() {
		this.keyPressed = UN_PRESSED;
		this.setKeyPressed(this.keyConf.getDown());
		this.setKeyPressed(this.keyConf.getLeft());
		this.setKeyPressed(this.keyConf.getRight());
		this.setKeyPressed(this.keyConf.getShift());
	}

	private void setKeyPressed(int keyPressed) {
		if (Input.isPressed(keyPressed)) {
			this.keyPressed = keyPressed;
		}
	}

	@Override
	public boolean isShift() throws RemoteException {
		return this.keyPressed == keyConf.getShift();
	}

	@Override
	public boolean isMoveLeft() throws RemoteException {
		return this.keyPressed == keyConf.getLeft();
	}

	@Override
	public boolean isMoveRight() throws RemoteException {
		return this.keyPressed == keyConf.getRight();
	}

	@Override
	public boolean isMoveDown() throws RemoteException {
		return this.keyPressed == keyConf.getDown();
	}

}